﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
using System.Collections;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace BLL
{
    public class UserBLL
    {
        static int id = 0;
        QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
        public bool check_login(string user, string pass)
        {
            var kq = from u in DB.Users
                     where u.MaCanBo == user && u.MatKhau == pass
                     select u;
            int kt = 0;
            kt = kq.Count();
            if (kt == 0)
                return false;
            else return true;
        }
        public string GetRole(string username)
        {
            var kq = DB.Users.Where(u => u.MaCanBo == username).FirstOrDefault();
            return kq.Role;
        }
        public IQueryable SelectDes()
        {
            return DB.Users.Join(
                DB.CanBos,
                a => a.MaCanBo,
                b => b.MaCanBo,
                (a, b) => new
                {
                    a.MaCanBo,
                    b.HoTen,
                    a.Role,
                    a.TrangThai
                }
                ).AsQueryable();
        }
        public string Insert(UserDTO user)
        {
            try
            {
                User u = new User();
                u.MaCanBo = user.Macanbo;// (id++).ToString();
                u.MatKhau = getMd5Hash(user.Matkhau);
                u.Role = user.Role;
                u.TrangThai = user.Trangthai;
                DB.Users.InsertOnSubmit(u);
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public static string getMd5Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        public string Update(UserDTO user)
        {
            try
            {
                User u = DB.Users.Where(s => s.MaCanBo == user.Macanbo).FirstOrDefault();
                u.MaCanBo = user.Macanbo;
                u.MatKhau = user.Matkhau;
                u.Role = user.Role;
                u.TrangThai = user.Trangthai;
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string Delete(string macanbo)
        {
            try
            {
                User user = DB.Users.Where(s => s.MaCanBo == macanbo).FirstOrDefault();
                DB.Users.DeleteOnSubmit(user);
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
     /*   public string layhoten(string macanbo)
        {
            QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
            return DB.CanBos.Where(s => s.MaCanBo == macanbo).Select(x => x.HoTen).FirstOrDefault();
        }
  */
    }
}